package com.itheima.tlias.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.tlias.exception.CustomException;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.pojo.Emp;
import com.itheima.tlias.pojo.EmpExpr;
import com.itheima.tlias.pojo.LoginInfo;
import com.itheima.tlias.service.EmpService;
import com.itheima.tlias.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("EmpServiceImpl")
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();
    }

    @Override
    public List<Emp> findSomeEmp(Emp emp, Date begin, Date end, int page, int pageSize) {
        // 使用 PageHelper 进行分页
        PageHelper.startPage(page, pageSize);
        return empMapper.findSomeEmp(emp, begin, end);

    }

    @Override
    public int delete(Integer... id) {
        int delete = empMapper.delete(id);
        int deleteEmpExpr = empMapper.deleteEmpExpr(id);
        if (deleteEmpExpr > 0 && delete > 0)
        return 1;
        return 0;
    }

    @Override
    @Transactional
    public int insert(Emp emp) {
        //判断用户名和姓名是否为空
        if (emp.getUsername() == null || emp.getUsername().trim().isEmpty() || emp.getName() == null || emp.getName().trim().isEmpty()){
            throw new CustomException("用户名和姓名不能为空");
        }
        int insert = 0;
        try {
            insert = empMapper.insert(emp);
        } catch (Exception e) {
            throw new CustomException("电话或账号重复");
        }
        if(emp.getExprList().size()<=0){
            throw new CustomException("请填写员工经历");
        }
        for (EmpExpr expr : emp.getExprList()) {
            expr.setEmpId(emp.getId());
        }
        empMapper.insertEmpExpr(emp.getExprList());
        return Math.max(insert, 0);
    }

    @Override
    public Emp findById(Integer id) {
        if (id > 0){
            Emp emp = empMapper.findById(id);
            List<EmpExpr> empExprById = empMapper.findEmpExprById(id);
            emp.setExprList(empExprById);
            return emp;
        }
        return null;
    }

    @Override
    @Transactional
    public int update(Emp emp) {
        //判断用户名和姓名是否为空
        if (emp.getUsername() == null || emp.getUsername().trim().isEmpty() || emp.getName() == null || emp.getName().trim().isEmpty()){
            throw new CustomException("用户名和姓名不能为空");
        }
        if (emp.getId() > 0){
            // 更新时间
            int update = empMapper.update(emp);
            // 删除老的
            empMapper.deleteEmpExpr(new Integer[]{emp.getId()});
            for (EmpExpr expr : emp.getExprList()) {
                expr.setEmpId(emp.getId());
            }
            if(emp.getExprList().size()<=0){
                throw new CustomException("请填写员工经历");
            }
            // 插入新的
            empMapper.insertEmpExpr(emp.getExprList());
            return update;
        }
        return 0;
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if(empLogin != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
}
